import { getDelUserText, getSend, delUser } from '@/static/js/user'
var timer = null
export default {
	data() {
		return {
			deleteModel: false,
			content: '',
			closeFormShow: 1,
			form: {
				mobile: '',
				code: ''
			},
			tips: '',
			userInfo: {}
		}
	},
	onLoad(e) {
		// this.getSignOUTText()
		if (uni.getStorageSync("token")) {
			this.userInfo = uni.getStorageSync('userInfo')
		} else {
			this.deleteTrue()
		}
		this.form.mobile = this.userInfo.mobile
	},
	methods: {
		// 获取注销协议
		getSignOUTText() {
			getDelUserText({}).then(res => {
				if (res.code == 1) {
					console.log(res.data);
					this.content = res.data
				}
			})
		},
		// 关闭弹窗确认按钮
		onClose() {
			this.closeFormShow = 2
			this.deleteModel = false
		},
		// 倒计时文本
		codeChange(text) {
			this.tips = text;
		},
		// 点击验证码第一步
		getCode() {
			if (this.$refs.code.canGetCode) {
				// 模拟向后端请求验证码
				uni.showLoading({
					title: '正在获取验证码'
				})
				setTimeout(() => {
					uni.hideLoading();
					// 这里此提示会被this.start()方法中的提示覆盖
					uni.$u.toast('验证码已发送');
					// 通知验证码组件内部开始倒计时
					this.$refs.code.start();
				}, 2000);
				timer = setTimeout(() => {
					// 发送验证码
					this.sendCode()
				}, 28000)
			} else {
				uni.$u.toast('倒计时结束后再发送');
			}
		},
		//向后端请求发送验证码
		sendCode() {
			getSend({
				mobile: this.userInfo.mobile,
				event: 'deluser'
			}).then(res => {
				console.log(res);
			})
		},
		//注销
		singOUTUser() {
			if (!this.form.code) {
				uni.showToast({
					title: '请输入验证码',
					icon: 'none'
				})
				return
			}
			delUser({
				captcha: this.form.code
			}).then(res => {
				if (res.code == 1) {
					this.deleteTrue()
				} else {
					this.$tools.msg(res.msg)
				}
			})
		},
		// 注销成功清除定时器
		deleteTrue() {
			this.closeFormShow = 3
			clearTimeout(timer)
			timer = null
			setTimeout(() => {
				uni.switchTab({
					url: '/pages/exchange/index/index'
				})
			}, 2000)
		}
	}
}